package chances.epg.user.event;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

import chances.epg.entity.user.ScheduleOrder;
import chances.wechat.service.GatewayService;

public class ScheduleEventListener implements ApplicationListener<ScheduleEvent> {
	
	@Autowired
	GatewayService gatewayService;

	@Override
	public void onApplicationEvent(ScheduleEvent event) {
		ScheduleOrder userSubscription = event.getUserScheduleOrder();
		this.send(userSubscription);
	}
	
	private void send(ScheduleOrder subscription) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("userId",subscription.getUserId());
		map.put("stbNo",subscription.getStbNo());
		map.put("type",subscription.getType());
		map.put("code",subscription.getCode());
		map.put("prog",subscription.getProg());
		map.put("title",subscription.getTitle());
		map.put("icon",subscription.getIcon());
		
		try {
			gatewayService.program(map);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
